package com.gzhryc.log.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.gzhryc.common.StringTools;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.log.dao.HttpVisitRecordDao;
import com.gzhryc.log.dao.InsideMessageDao;
import com.gzhryc.log.dao.db.InsideMessage;
import com.gzhryc.log.services.dto.InsideMessageSearch;

public class InsideMessageService {

	public InsideMessageService self() {
		return MultiDBTools.getService(InsideMessageService.class, true);
	}

	public InsideMessageService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,InsideMessageService.class, true);
	}

	InsideMessageDao dao;

	public InsideMessageService(String jdbcKey) {
		this.dao = new InsideMessageDao(jdbcKey);
	}
	
	public Long countBySearch(InsideMessageSearch search) {
		Conditions condition = new Conditions(InsideMessage.class);
		condition.and().is("userId", search.getUserId());
		condition.and().is("status", search.getStatus());
		condition.asc("status");
		try {
			return dao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}

	public List<InsideMessage> findBySearch(InsideMessageSearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = new Conditions(InsideMessage.class);
		condition.and().is("userId", search.getUserId());
		condition.and().is("status", search.getStatus());
		condition.asc("status");
		try {
			return dao.findByCondition(condition,pageNo,pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<InsideMessage> findBySearch(InsideMessageSearch search){
		Conditions condition = new Conditions(InsideMessage.class);
		condition.and().is("userId", search.getUserId());
		condition.and().is("status", search.getStatus());
		try {
			return dao.findByCondition(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public boolean add(InsideMessage insideMessage) {
		insideMessage.setCreateDate(new Date());
		boolean b =false;
		try {
			b=dao.insert(insideMessage)>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	
	public boolean  update(InsideMessage insideMessage) {
		boolean b=false;
		try {
			b=dao.updateNotNull(insideMessage)>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	
	public boolean delete(Long id) {
		boolean b=false;
		try {
			b=dao.deleteById(id)>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	
	public InsideMessage getById(Long id) {
		try {
			return dao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean batchdel(String ids) {
		boolean b =false;
		List<Long> idss = StringTools.split2Long(ids,",");
		Conditions conditions = new Conditions(InsideMessage.class);
		conditions.and().in("id", idss);
		try {
			b=dao.deleteByCondition(conditions)>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
	public boolean batchEdit(String ids,InsideMessage insideMessage) {
		boolean b =false;
		List<Long> idss = StringTools.split2Long(ids,",");
		Conditions conditions = new Conditions(InsideMessage.class);
		conditions.and().in("id", idss);
		try {
			b=dao.updateNotNull(insideMessage, conditions)>0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return b;
	}
}
